<?php
namespace Controller;

use Slim\Http\Request;
use Slim\Http\Response;
use Slim\Container;
use Intervention\Image\ImageManagerStatic as Image;


class ActivityApi extends Base
{

    public function __construct(Container $container)
    {
        parent::__construct($container);
        if ($this->getaccount() === false) {
            header("HTTP/1.1 400 Bad Request");
            echo 'no account error';
            exit;
        }else{
            $this->account=$this->getaccount();
        }
    }





    public function createactivity(Request $req, Response $res)
    {
        $validate = $this->guard->validateToken($req->getParam('tk_name'), $req->getParam('tk_value'));
        if ($validate === true) {
            if ($UploadedFiles = $req->getUploadedFiles()) {
                $file = $UploadedFiles['media'];
                $Size = $file->getSize();
                $name = explode('.', $file->getClientFilename());
                $checksize = $this->checksize($Size, 10);
                if ($checksize === false) {
                    return $res->withStatus(400)->write('文件过大');
                    exit;
                }

                $filetype = ['jpg', 'png', 'jpeg', 'JPG', 'PNG', 'image/jpeg', 'image/png'];
                if (in_array($name[1], $filetype) === false) {
                    return $res->withStatus(400)->write('文件类型错误');
                    exit;
                }
                $filepath = $this->checkuploadfile();
                $newname = uniqid() . '.' . $name[1];
                $movefile = $filepath['filepath'] . '/' . $newname;
                $movethumbfile = $filepath['thumbfilepath'] . '/' . $newname;
                Image::make($file->file)->resize(100, 100)->save($movethumbfile);
                $file->moveTo($movefile);
            } else {
                $newname = '';
            }


            $id = $req->getParam('act_id');
            $activityname = $req->getParam('activityname');
            $minage = $req->getParam('minage');
            $maxage = $req->getParam('maxage');
            $starttime = $req->getParam('starttime');
            $endtime = $req->getParam('endtime');
            $maxnum = $req->getParam('maxnum');
            $maxintegral = $req->getParam('maxintegral');
            $content = $req->getParam('content');

            if ($this->checkfrom->regex('require', $activityname) === false) {
                return $res->withStatus(400)->write('活动名称错误');
                exit;
            }
            if ($this->checkfrom->regex('number', $minage) === false || $this->checkfrom->regex('number', $maxage) === false) {
                return $res->withStatus(400)->write('适合年龄错误');
                exit;
            }
            if ($this->checkfrom->regex('require', $starttime) === false || $this->checkfrom->regex('require', $endtime) === false) {
                return $res->withStatus(400)->write('活动时间错误');
                exit;
            }
            if ($this->checkfrom->regex('number', $maxnum) === false) {
                return $res->withStatus(400)->write('招募家庭错误');
                exit;
            }
            if ($this->checkfrom->regex('number', $maxintegral) === false) {
                return $res->withStatus(400)->write('积分兑换错误');
                exit;
            }
            if ((int)$minage > (int)$maxage) {
                return $res->withStatus(400)->write('年龄范围顺序错误');
                exit;
            }
            if (strtotime($starttime) > strtotime($endtime)) {
                return $res->withStatus(400)->write('活动时间顺序错误');
                exit;
            }
            $data = [
                'cur_id' => $this->account['id'],
                'activityname' => $activityname,
                'minage' => $minage,
                'maxage' => $maxage,
                'starttime' => strtotime($starttime),
                'endtime' => strtotime($endtime),
                'maxnum' => $maxnum,
                'maxintegral' => $maxintegral,
                'content'=>$this->remove_xss($content),
                'createtime' => time(),
                'updatetime' => time()

            ];
            if (!empty($newname)) {
                $data=array_merge($data, ['media' => $newname]);
            }
            if (empty($id)) {
                $count = $this->db->insert('tcp_activity', $data);
            } else {
                $result = $this->db->where(['id' => $id])->find('tcp_activity');
                if (!empty($result)) {
                    unset($data['createtime']);
                    $count = $this->db->where(['id' => $id])->update('tcp_activity', $data);
                } else {
                    return $res->withStatus(400)->write('data error');
                    exit;
                }

            }
            if (!empty($count)) {
                return $res->withStatus(200)->write('保存成功');
                exit;
            } else {
                return $res->withStatus(400)->write('保存失败');
                exit;
            }

        } else {
            return $res->withStatus(400)->write('guard error');
            exit;
        }


    }

    public function deleteactivity(Request $req, Response $res)
    {
        $id = $req->getParam('id');
        $arr=explode(',',$id);
        $error=0;
        foreach ($arr as $val){
            if($this->checkfrom->regex('number', (int)$val) === false){
                $error++;
                continue;
            }
        }
        if(empty($error)){
            $where['id']=array("(".$id.")",'in');
            $count = $this->db->where($where)->update('tcp_activity', ['is_delete'=>1,'updatetime'=>time()]);
            if (!empty($count)) {
                return $res->withStatus(200)->write('删除成功');
                exit;
            } else {
                return $res->withStatus(400)->write('删除失败');
                exit;
            }
        }else{
            return $res->withStatus(400)->write('data error');
            exit;
        }




    }

}